package com.wcgksoft.controller.question;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.question.AdviceType;
import com.wcgksoft.entity.question.QType;
import com.wcgksoft.service.question.AdviceTypeService;
import com.wcgksoft.service.question.QTypeService;
import net.sf.json.JSONObject;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @author 御风
 * @version V1.0
 * @Package com.wcgksoft.controller.question
 * @Description TODO
 * @date 2020/5/24 19:12
 * @ClassName AdviceController
 */
@Controller
@RequestMapping("adviceType")
public class AdviceController {

    @Autowired
    private AdviceTypeService adviceTypeService;

    @ResponseBody
    @RequestMapping(value = "findAll")
    public Layui findAll(@RequestParam(value = "page",required = false)Integer page,
                         @RequestParam(value = "limit",required = false)Integer limit){
        DetachedCriteria criteria = DetachedCriteria.forClass(AdviceType.class);
        List<AdviceType> list = adviceTypeService.findByConditions(criteria, page, limit);
        return Layui.data(0, "success", adviceTypeService.getTotalCount(), list);
    }

    @ResponseBody
    @RequestMapping(value = "findAllType")
    public Layui findAllType(){
        DetachedCriteria criteria = DetachedCriteria.forClass(AdviceType.class);
        List<AdviceType> list = adviceTypeService.findByConditions(criteria);
        return Layui.data(0, "success", adviceTypeService.getTotalCount(), list);
    }

    @ResponseBody
    @RequestMapping(value = "update")
    public Layui update(@RequestParam(value = "data",required = false)String data){
        JSONObject param = JSONObject.fromObject(data);
        AdviceType adviceType=null;
        if (null==param){
            return Layui.data(1, "请求格式有误", 0, null);
        }else{
            adviceType=(AdviceType) JSONObject.toBean(JSONObject.fromObject(data), AdviceType.class);
            adviceType.setAddTime(new Date());
            adviceTypeService.save(adviceType);
            return Layui.data(0,"success",1,Layui.addList(adviceType));
        }
    }

    @ResponseBody
    @RequestMapping(value = "delete")
    public Layui delete(@RequestParam(value = "id",required = false) Integer id){
        try {
            AdviceType byId = adviceTypeService.findById(id);
            adviceTypeService.delete(byId);
            return Layui.data(0,"操作成功",0,null);
        }catch (Exception e){
            e.printStackTrace();
            return Layui.data(0,"删除失败",0,null);
        }

    }

}
